#include <stdio.h>
#include <string.h>
#include "ex8.h"

void ex1(){
    int a[101] = {0};
    int i,j,n ;
    for (i=0;i<101;i++) a[i]=i;

    for (i=j=2;i<101;i++){
        if (a[i]) {
            while (i*j<101) {
                a[i*j] = 0;
                j++;
            }
            j=2;
        }
    }

    for (i=2,n=0;i<101;i++){
        if (a[i]){
            printf("%5d",a[i]);
            n++;
            }
        if (n==10){
            printf("\n");
            n=0;
        }
    }
    printf("\n");
}

void ex2(){
    int a[10] = {2,94,62,13,56,8,6,45,95,34};
    int temp = 0;
    int * p1, * p2;

    for (p1=a+10;p1>a;p1--){
        for (p2=a;p2<p1;p2++){
            if (*p2>*p1){
                temp=*p2,*p2=*p1,*p1=temp;
            }
        }
    }
    for (p1=a;p1<a+10;){
        printf("%d\n",*p1++);
    }
}

void ex3(){
    int a[3][3] = {0};
    int (* r)[3] = a, i, sum=0;
    for (i=0;i<3;i++){
        a[i][i] = (i+2)*(i+5); // 10+18+28
    }
    for (i=0;r<a+3;r++,i++){
        sum += *(*r +i);
    }
    printf("%d\n",sum);
}

void ex4(){
    int a[11] = {0};
    int i,m, * p;
    for (i=0;i<10;i++){
        a[i] = (i+1)*2;
    }
    printf("insert:");
    scanf("%d",&m);
    for (p=a+9;p>=a;p--){
        if (*p < m){
            *(p+1) = m;
            break;
        }
        *(p+1) = *p;
    }
    if (p==a-1) *(p+1)=m;
    for (i=0;i<11;i++){
        printf("%d ",a[i]);
    }
}

void ex5(){
    int a[11] = {0};
    int i, *p=a, *q=a+10;
    for (i=0;i<11;i++) a[i]=i+1;
    while (p<q){
        i=*p,*p = *q,*q=i;
        p++,q--;
    }
    for (i=0;i<11;i++) printf("%d ",a[i]);
}

void ex6(){
    int l1[10] = {1,0};
    int l2[11] = {0};
    int i=0,j=0;
    while (i<10){
        for (j=0;j<10;j++){
            if (l1[j]) printf("%5d",l1[j]);
            l2[j+1]=l1[j];
        }
        printf("\n");
        for (j=0;j<10;j++)
            l1[j] += l2[j];
        i++;
    }
}

void ex7(){
    int arr[15][15] = {0};
    int n=1,i,r=0,c;
    printf("n(1~15):");
    scanf("%d",&n);
    c = (n+1)/2-1;
    for (i=0;i<n*n;i++){
        arr[r][c] = i+1;
        r=(n+r-1)%n,c=(c+1)%n;
        if (arr[r][c]){ // back
            r=(r+2)%n;
            c=(n+c-1)%n;
        }
    }
    for (r=0;r<n;r++){
        for (c=0;c<n;c++)
            printf("%5d",arr[r][c]);
        printf("\n");
    }
}

void ex9(){
    int a[10]={0};
    int i,n, *p=a, *q=a+9,*med, flag=0;
    for (i=0;i<10;i++) a[i]=i+1;
    printf("find:");
    scanf("%d",&n);
    while (p+1<q){
        if (*q==n){
            flag=1;
            med=q;
            break;
            }
        med = p + (q-p)/2;
        if (*med>n) q=med;
        else if (*med<n) p=med;
        else {
            flag=1;
            break;
            }
    }
    if (flag) printf("find %d at %ld\n",*med,med-a);
    else printf("can't find");
}

void ex11(){
    char line[]="*****";
    int i = 0,j;
    while (line[i]!='\0'){
        for (j=0;j<i;j++) printf(" ");
        printf("%s\n",line);
        ++i;
    }
} 

void ex12(){
    char ling[80];
    char * q=ling;
    fgets(ling,sizeof ling,stdin);
    while (*q!='\0'){
        if (*q<='z' && *q>='a') *q='a'+'z'-*q;
        if (*q<='Z' && *q>='A') *q='A'+'Z'-*q;
        ++q;
    }
    printf("%s\n",ling);
}

void ex13(){
    char s1[80],s2[40];
    char *p=s1,*q=s2;
    printf("input string:\n");
    fgets(s1,sizeof s1,stdin);
    printf("input string:\n");
    fgets(s2,sizeof s2,stdin);
    // strcat(s1,s2);
    while (*p!='\0') ++p;
    while (*q!='\0') *p++=*q++;
    *p='\0';
    printf("%s\n",s1);
}

void ex14(){
    char s1[40],s2[40];
    char *p=s1,*q=s2;
    printf("input string:\n");
    fgets(s1,sizeof s1,stdin);
    printf("input string:\n");
    fgets(s2,sizeof s2,stdin);
    while (*q!='\0'&&*p!='\0'&&*q==*p) ++p,++q;
    if (*q=='\0'|| *p=='\0' ||*q==*p) printf("%d\n",0);
    else printf("%d\n",*p-*q);
}

void ex15(){
    char s1[40],s2[40];
    char *p=s1,*q=s2;
    printf("input string:\n");
    fgets(s1,sizeof s1,stdin);
    printf("input string:\n");
    fgets(s2,sizeof s2,stdin);
    // strcpy(s1,s2);
    while (*q!='\0')*p++=*q++;
    *p='\0';
    printf("%s\n",s1);
}

int main(){
    // printf("ex1:\n");
    // ex1();
    // printf("ex2:\n");
    // ex2();
    // printf("ex3:\n");
    // ex3();
    // printf("ex4:\n");
    // ex4(); 
    // printf("ex5:\n");
    // ex5();
    // printf("ex6:\n");
    // ex6();
    // printf("ex7:\n");
    // ex7();
    // printf("ex8:\n");
    // ex8();
    // printf("ex9:\n");
    // ex9();
    // printf("ex11:\n");
    // ex11();
    // printf("ex12 de/encode:\n");
    // ex12();
    // printf("ex13 cat:\n");
    // ex13();
    // printf("ex14 compare:\n");
    // ex14();
    // printf("ex15 copy:\n");
    // ex15();
    return 0;
}